<?php
declare(strict_types=1);

namespace app\api\service\home;


use app\api\model\game\Category;
use app\api\service\User as UserService;
use app\common\model\HomeCallLog;
use app\common\service\BaseService;
use think\facade\Log;
use app\api\model\game\Game as GameModel;

class Game extends BaseService
{
    //首页列表
    public function getList($params):array
    {
        //todo 记录日志 方便统计登录情况
        $this->statistics();
        $condition = [];
        if(!empty($params['category_id'])){
            $condition[] = ['category_id','=',$params['category_id']];
        }
        return (new GameModel())
            ->with(['room'])
            ->where($condition)
            ->order('sort', 'DESC')
            ->paginate($params['limit'] ?? 15)
            ->toArray();
    }


    //统计调用
    public function statistics(): bool
    {
        try {
            $access_token = $this->request->header('access-token','');
            if($access_token){
                $user_id = UserService::getCurrentLoginUserId();
                $condition = [
                    'user_id' => $user_id,
                    'call_day' => date('Y-m-d')
                ];
                $today = (new HomeCallLog())->where($condition)->find();
                if(!$today){
                    HomeCallLog::create([
                        'user_id' => $user_id,
                        'call_time' => time(),
                        'call_day' => date('Y-m-d')
                    ]);
                }
            }
            return true;
        }catch (\Exception $e){
            Log::info($e->getMessage());
            return false;
        }
    }

    //首页表头分类
    public function getCategoryList(): array
    {
        $condition = [
            ['is_del','=',0]
        ];
        return (new Category())
            ->where($condition)
            ->order('sort','DESC')
            ->column(['id','name']);
    }





}